﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;

public partial class ExternalCVForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Retrieve unique id and secret lye concentration from query strings passed into URL
        // eg. ExternalCVForm.aspx?uID=XXX&lyeConcentration=XXX
        string uID = Request.QueryString["uID"];
        string lyeConcentration = Request.QueryString["lyeConcentration"];

        // No message to show user
        statusLabel.Text = "";
        
        // Verify that the user is allowed to view this page
        bool uIDVerify = long.TryParse(uID, out lyeConcentrationLong);
        bool lyeVerify = long.TryParse(lyeConcentration, out uIDLong);


        if (uIDVerify && lyeVerify)
        {
            uIDLong = long.Parse(uID);
            lyeConcentrationLong = long.Parse(lyeConcentration);
        }
    }

    
    public long uIDLong = 0;
    public long lyeConcentrationLong = 0;

    /// <summary>
    /// This method handles uploading a PDF file to the database and provides error checking.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void uploadClick(object sender, EventArgs e)
    {
        if (cvUploader.HasFile)
        {

            if (Lutefisk.VerifySecretLyeConcentration(uIDLong, lyeConcentrationLong))
            {
                try
                {
                    if (cvUploader.PostedFile.ContentType == "application/pdf")
                    {
                        if (cvUploader.PostedFile.ContentLength < (102400 * 10))
                        {
                            string filename = Path.GetFileName(cvUploader.FileName);

                            // Save file to the server
                            cvUploader.SaveAs(Server.MapPath("~/") + "PDFs/" + uIDLong.ToString() + ".pdf");

                            // Update the Musicians SQL Database record to specify that they have a CV.
                            string sqlInsert = "UPDATE musicians SET cv = 1 WHERE uid = '" + uIDLong.ToString() + "' ;";

                            //create an SQL connection.
                            using (SqlConnection dbconn = new SqlConnection(Lutefisk.CONNECTION_STRING))
                            {
                                dbconn.Open();

                                //execute the command
                                using (SqlCommand cmd = new SqlCommand(sqlInsert, dbconn))
                                {
                                    try
                                    {
                                        cmd.ExecuteNonQuery();
                                    }
                                    catch (SqlException ax)
                                    {
                                        Console.WriteLine(ax);
                                    }
                                }
                            }

                            // Now that the user has a CV change the screen to display to the user that the
                            // upload was a success.
                            successPanel.Visible = true;
                            uploadPanel.Visible = false;
                        }
                        else
                        {
                            statusLabel.Text = "Upload Failure. The file has to be less than 1MB!";
                        }
                    }
                    else
                    {
                        statusLabel.Text = "Upload Failure. Client does not recognize the document as a PDF.";
                    }
                }
                catch (Exception ex)
                {
                    statusLabel.Text = "Upload Failure. The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
            else
            {
                statusLabel.Text = "I cannot do this. Lye concentrations do not match! Are you trying to SpoooOOooooOOOof me?";
            }
        }
        else
        {
            statusLabel.Text = "Upload Failure. No file selected.";
        }
    }
}